package com.upup.cloud.insert.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;  
import org.springframework.data.redis.connection.RedisConnectionFactory;  
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;
/**
 * @author yangzhe
 * 配置多个Redis
 * @description
 */  
@Configuration  
public class RedisConfig {  
  
    @Value("${spring.redis.host}")  
    private String hostName;  
    @Value("${spring.redis.port}")  
    private int port;  
    @Value("${spring.redis.password}")  
    private String passWord;  
    @Value("${spring.redis.jedis.pool.maxIdle}")
    private int maxIdl;  
    @Value("${spring.redis.jedis.pool.minIdle}")
    private int minIdl;  
    @Value("${spring.redis.timeout}")
    private int timeout;  
    @Value("${spring.redis.host2}")  
    private String hostName2;
    @Value("${spring.redis.port2}")
    private int port2;
    @Value("${spring.redis.password2}")
    private String passWord2;

    @Bean  
    public RedisConnectionFactory redisConnectionFactory(){
        JedisPoolConfig poolConfig=new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdl);  
        poolConfig.setMinIdle(minIdl);  
        poolConfig.setTestOnBorrow(true);  
        poolConfig.setTestOnReturn(true);  
        poolConfig.setTestWhileIdle(true);  
        poolConfig.setNumTestsPerEvictionRun(10);  
        poolConfig.setTimeBetweenEvictionRunsMillis(60000);  
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);  
        jedisConnectionFactory.setHostName(hostName);  
        if(!passWord.isEmpty()){  
            jedisConnectionFactory.setPassword(passWord);  
        }  
        jedisConnectionFactory.setPort(port);  
        return jedisConnectionFactory;
    }  
  
    @Bean  
    public RedisConnectionFactory redisConnectionFactory2(){  
        JedisPoolConfig poolConfig=new JedisPoolConfig();  
        poolConfig.setMaxIdle(maxIdl);  
        poolConfig.setMinIdle(minIdl);  
        poolConfig.setTestOnBorrow(true);  
        poolConfig.setTestOnReturn(true);  
        poolConfig.setTestWhileIdle(true);  
        poolConfig.setNumTestsPerEvictionRun(10);  
        poolConfig.setTimeBetweenEvictionRunsMillis(60000);  
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(poolConfig);  
        jedisConnectionFactory.setHostName(hostName2);  
        if(!passWord2.isEmpty()){
            jedisConnectionFactory.setPassword(passWord2);
        }  
        jedisConnectionFactory.setPort(port2);
        return jedisConnectionFactory;
    }  
  
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplateObject() throws Exception {  
        RedisTemplate<String, Object> redisTemplateObject = new RedisTemplate<String, Object>();  
        redisTemplateObject.setConnectionFactory(redisConnectionFactory());  
        redisTemplateObject.afterPropertiesSet();
        return redisTemplateObject;  
    }  
  
    @Bean(name = "redisTemplate2")  
    public RedisTemplate<String, Object> redisTemplateObject2() throws Exception {  
        RedisTemplate<String, Object> redisTemplateObject = new RedisTemplate<String, Object>();  
        redisTemplateObject.setConnectionFactory(redisConnectionFactory2());  
        redisTemplateObject.afterPropertiesSet();
        return redisTemplateObject;  
    }  
  
  

}  